/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.NiftyMethodInvoker;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.ElementRenderer;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

/**
 * @date 2024年7月11日19点45分
 * @author JohnKkk
 */
public class NiftyGui03 extends SimpleApplication implements ScreenController{

    private Nifty m_Nifty;
    private float m_TPF;
    public static void main(String[] args) {
        NiftyGui03 niftyGui03 = new NiftyGui03();
        niftyGui03.start();
    }

    @Override
    public void simpleInitApp() {
        // 初始化Nifty
        NiftyJmeDisplay niftyDisplay = NiftyJmeDisplay.newNiftyJmeDisplay(
                assetManager,
                inputManager,
                audioRenderer,
                guiViewPort);
        m_Nifty = niftyDisplay.getNifty();
        // 将NiftyGUI显示对象添加到JME3中
        guiViewPort.addProcessor(niftyDisplay);
        
        m_Nifty.fromXml("Interface/NiftyGui03.xml", "screen0", this);
        
        // 禁用flyCam并显示鼠标
        flyCam.setEnabled(false);
        inputManager.setCursorVisible(true);
    }
    
    /**
     * 返回当前标题内容.<br/>
     * @return 用于要在xml中id=title的label显示
     */
    public String getTitle(){
        return "tpf:" + m_TPF;
    }

    @Override
    public void simpleUpdate(float tpf) {
        super.simpleUpdate(tpf); //To change body of generated methods, choose Tools | Templates.
        m_TPF = tpf;
    }
    
    /**
     * 添加一个public void函数,用于NiftyGui xml绑定.<br/>
     * @param text 传递的参数
     */
    public void testClickEvent(String text){
        System.out.println("点击按钮,传递的参数:" + text);
        m_Nifty.gotoScreen("screen1");
    }
    /**
     * 测试函数.<br/>
     * @param text 传递的参数
     */
    public void testClickEvent2(String text){
        System.out.println("执行测试函数:" + text);
    }

    // --------------------------------ScreenController接口-----------------------------------↓
    @Override
    public void bind(Nifty nifty, Screen screen) {
        System.out.println("bind screen : " + screen.getScreenId());
//        Element button0 = screen.findElementById("button0");
//        for(ElementRenderer elementRenderer : button0.getElementRenderer()){
//            System.out.println("elementRender:" + elementRenderer.toString());
//        }
//        // 通过Java代码重新绑定button0的onClick事件回调
//        button0.getElementInteraction().getPrimary().setOnClickMethod(new NiftyMethodInvoker(nifty, "testClickEvent2(111)", this));
    }

    @Override
    public void onStartScreen() {
        System.out.println("onStartScreen------------------!");
    }

    @Override
    public void onEndScreen() {
        System.out.println("onEndScreen------------------!");
    }
    // ---------------------------------ScreenController接口----------------------------------↑
    
}
